package com.m2bshop.shop.controller;

import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSONArray;
import com.m2bshop.shop.pojo.Brand;
import com.m2bshop.shop.pojo.Result.Result;
import com.m2bshop.shop.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @RequestMapping("/getbrand")
    @ResponseBody
    public Result brandJSON(@RequestBody Map map,Model model){
        System.out.println("get brand "+map.get("companyId").toString());
        List<Brand> brands = brandService.findBrandByComId(Integer.parseInt(map.get("companyId").toString()));

        System.out.println(brands);
        model.addAttribute("brand",brands);
        return Result.ok(brands);
    }

    @RequestMapping("/addbrand")
    public Result addbrand(MultipartFile uploadFile,HttpServletRequest request){
//        brandService.uploadFile(uploadFile,request);
        System.out.println("add");
        return Result.ok();
    }

    @PostMapping("/upload")
    @ResponseBody
    public Result uploadFile(@RequestParam(value = "file") MultipartFile file, @RequestParam(value = "brandName")String brandName) {
//        brandService.uploadFile(uploadFile,request);
        String imgPath = "D:/web/img/";//获取图片路径

        String ImgName = file.getOriginalFilename();
        String lastName = ImgName.substring(ImgName.lastIndexOf(".")); //获取图片后缀名
        String newName = brandName+lastName;//修改图片名字 用户 id+之前图片后缀名(.png)
        File filePath = new File(imgPath,newName);//整合图片路径

        // 判断路径是否存在，如果不存在就创建一个
        if (!filePath.getParentFile().exists()) {
            filePath.getParentFile().mkdirs();
        }
        // 将上传的文件保存到一个目标文件当中
        try {
            file.transferTo(filePath);
            Brand brand = new Brand();
            brand.setBrandName("123");
            brand.setLogo("http://localhost:8011/img/"+newName);
            brand.setCompanyId(1);
            brandService.addBrand(brand);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return Result.ok();


    }


    @RequestMapping("/delbrand")
    @ResponseBody
    public Result deleteBrand(@RequestBody Map map){
        System.out.println("map:"+map);
        String state = map.get("state").toString();
        int brandId = Integer.parseInt(map.get("brandId").toString());
        System.out.println(brandId+state);
        brandService.updateBrandState(brandId,state);
//        storeService.updateStoreState(Integer.parseInt(map.get("pkStore").toString()), map.get("state").toString());
        return Result.ok();
    }

    /**
     * 根据页面名称，控制器跳转页面
     * @param name 页面名称
     * @param model
     * @return 页面.html
     */
    @RequestMapping("/go{name}")
    public String jumpHtml(@PathVariable String name, Model model){
        System.out.println(name);
        return name;
    }

}
